package 并发编程.proxy;


import sun.misc.ProxyGenerator;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ProxyUtil {
    public static void generateClassFile(Class clazz, String proxyName) {
        /*
         * Generate the specified proxy class.
         */
        byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
                proxyName, new Class[]{clazz});
        String path = clazz.getResource("/").getPath();
        System.out.println("path = " + path);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path + proxyName + ".class");
            out.write(proxyClassFile);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
